package com.foreveross.proxyip.infra;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.junit.Before;
import org.junit.Test;

import com.dayatang.utils.DateUtils;
import com.foreveross.proxyip.infra.db.mongo.IMongoRepository;
import com.foreveross.proxyip.infra.db.mongo.MongoRepositoryImpl;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;

/**
 * ip重复率统计
 * 
 * @author fb
 */
public class IpRepetitionStatistics {
	
	private IMongoRepository mr; 
	
	@Before
	public void init() { 
		mr = new MongoRepositoryImpl("proxyip");
	}
	 
	@SuppressWarnings("unchecked")
	@Test
	public void statistics() {
		 mr = mr == null ? new MongoRepositoryImpl("proxyip") : mr;
		 BasicDBObject query = new BasicDBObject();
	     query.append("create_time", BasicDBObjectBuilder.start("$gte", DateUtils.parseDateTime("2014-09-18 15:00:00")).add("$lte", DateUtils.parseDateTime("2014-09-18 15:31:00")).get());
		 Map<String,List<String>> sorces = new HashMap<String, List<String>>();
		 List<List<String>> values = new ArrayList<List<String>>();
		 List<String> keys = new ArrayList<String>();
		 Map<String, List<String>> result = new HashMap<String, List<String>>();
		 Set<String> removal = null;
		 Set<String> effectiveIp = new HashSet<String>();
		 long start = System.currentTimeMillis();
		 List<DBObject> dbos = mr.query(IpStoreEntity.class, query);
		 long end = System.currentTimeMillis();
		 
		 log.debug("ip数量:" + dbos.size() + ", 耗时：" + (end - start) + "\r\t");
		 log.debug("--------------------------------------------");
		 
		 for (DBObject db : dbos) {
			 List<String> list = sorces.get(db.get("source"));
			 
			 if (list == null) {
				 list = new ArrayList<String>();
			 }
			 
			 list.add(db.get("ip").toString().trim() + ":" + db.get("port").toString().trim());
			 sorces.put(db.get("source").toString().trim(), list);
		 }
		 
		 // 数据结构整理，方便组合
		 for(Entry<String, List<String>> entry : sorces.entrySet()) {
			keys.add(entry.getKey());
			values.add(entry.getValue());
		 }
		 
		 for (int i = 0; i < keys.size(); i++) {
			for (int j = i; j < keys.size(); j++) {
				if (i == j) {
					//result.put(keys.get(i) + "--" + keys.get(j),  new ArrayList<String>(new HashSet<String>(values.get(i))));
					//result.put(keys.get(i) + "--" + keys.get(j), ((List<String>) CollectionUtils.subtract(values.get(i), new HashSet<String>(values.get(j)))));
					continue;
				}
				
				result.put(keys.get(i) + "--" + keys.get(j), ((List<String>) CollectionUtils.intersection(values.get(i), values.get(j))));
			}
		 }
		 
		 end = System.currentTimeMillis();
		 
		 log.debug("分析完成总共耗时：" + (end - start) + "\r\t");
		 
		 log.debug("--------------------------------------------");
		 
		 for (int i = 0; i < keys.size(); i++) {
			 removal = new HashSet<String>(values.get(i));
			 effectiveIp.addAll(removal);
			 log.debug(keys.get(i) + " : " + values.get(i).size() + ", 去重后：" + removal.size() + ", 重复数：" + (values.get(i).size() - removal.size()));
		 }
		 
		 System.out.printf("下载ip总数:%s, 不重复ip数%s", dbos.size(), effectiveIp.size());
		 
		 log.debug("\r\t--------------------------------------------");
		 
		 for (Entry<String, List<String>> entry : result.entrySet()) {
			 log.debug(entry.getKey() + " :  " + entry.getValue().size());
		 }
	}

	public static void main(String[] args) { 
		IpRepetitionStatistics irs = new IpRepetitionStatistics();
		irs.statistics();
	}
}
